package com.example.kotlintest.chat;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;

import com.example.kotlintest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author linzi
 * @date 2022-9-28
 */
public class EmojiUtils {
    private static final String TAG = "EmojiUtils";
    public static int[] res=new int[]{
            R.drawable.emoji_00,
            R.drawable.emoji_01,
            R.drawable.emoji_02,
            R.drawable.emoji_03,
            R.drawable.emoji_04,
            R.drawable.emoji_05,
            R.drawable.emoji_06,
            R.drawable.emoji_07,
            R.drawable.emoji_08,
            R.drawable.emoji_09,
            R.drawable.emoji_10,
            R.drawable.emoji_11,
            R.drawable.emoji_12,
            R.drawable.emoji_13,
            R.drawable.emoji_14,
            R.drawable.emoji_15,
            R.drawable.emoji_16,
            R.drawable.emoji_17,
            R.drawable.emoji_18,
            R.drawable.emoji_19,
            R.drawable.emoji_20,
            R.drawable.emoji_21,
            R.drawable.emoji_22,
            R.drawable.emoji_23,
            R.drawable.emoji_24,
            R.drawable.emoji_25,
            R.drawable.emoji_26,
            R.drawable.emoji_27,
            R.drawable.emoji_28,
            R.drawable.emoji_29,
            R.drawable.emoji_30,
            R.drawable.emoji_31,
            R.drawable.emoji_32,
            R.drawable.emoji_33,
            R.drawable.emoji_34,
            R.drawable.emoji_35,
            R.drawable.emoji_36,
            R.drawable.emoji_37,
            R.drawable.emoji_38,
            R.drawable.emoji_39,
            R.drawable.emoji_40,
            R.drawable.emoji_41,
            R.drawable.emoji_42,
            R.drawable.emoji_43,
            R.drawable.emoji_44,
            R.drawable.emoji_45,
            R.drawable.emoji_46,
            R.drawable.emoji_47,
            R.drawable.emoji_48,
            R.drawable.emoji_49,
            R.drawable.emoji_50,
            R.drawable.emoji_51,
            R.drawable.emoji_52,
            R.drawable.emoji_53,
            R.drawable.emoji_54,
            R.drawable.emoji_55,
            R.drawable.emoji_56,
            R.drawable.emoji_57,
            R.drawable.emoji_58,
            R.drawable.emoji_59,
            R.drawable.emoji_60,
            R.drawable.emoji_61,
            R.drawable.emoji_62,
            R.drawable.emoji_63,
            R.drawable.emoji_64,
            R.drawable.emoji_65,
            R.drawable.emoji_66,
            R.drawable.emoji_67,
            R.drawable.emoji_68

    };
    public static String[] msg=new String[]{
                    "[大笑]" ,
                    "[开心]" ,
                    "[色]" ,
                    "[酷]" ,
                    "[奸笑]" ,
                    "[亲]" ,
                    "[伸舌头]" ,
                    "[眯眼]" ,
                    "[可爱]" ,
                    "[鬼脸]" ,
                    "[偷笑]" ,
                    "[喜悦]" ,
                    "[狂喜]" ,
                    "[惊讶]" ,
                    "[流泪]" ,
                    "[流汗]" ,
                    "[天使]" ,
                    "[笑哭]" ,
                    "[尴尬]" ,
                    "[惊恐]" ,
                    "[大哭]" ,
                    "[烦躁]" ,
                    "[惊讶]" ,
                    "[两眼冒星]" ,
                    "[害羞]" ,
                    "[睡着]" ,
                    "[冒星]" ,
                    "[口罩]" ,
                    "[OK]" ,
                    "[好吧]" ,
                    "[鄙视]" ,
                    "[难受]" ,
                    "[不屑]" ,
                    "[不舒服]" ,
                    "[愤怒]" ,
                    "[鬼怪]" ,
                    "[发怒]" ,
                    "[生气]" ,
                    "[不高兴]" ,
                    "[皱眉]" ,
                    "[心碎]" ,
                    "[心动]" ,
                    "[好的]" ,
                    "[低级]" ,
                    "[赞]" ,
                    "[鼓掌]" ,
                    "[给力]" ,
                    "[打你]" ,
                    "[阿弥陀佛]" ,
                    "[拜拜]" ,
                    "[第一]" ,
                    "[拳头]" ,
                    "[手掌]" ,
                    "[剪刀]" ,
                    "[招手]" ,
                    "[不要]" ,
                    "[举着]" ,
                    "[思考]" ,
                    "[猪头]" ,
                    "[不听]" ,
                    "[不看]" ,
                    "[不说]" ,
                    "[猴子]" ,
                    "[炸弹]" ,
                    "[睡觉]" ,
                    "[筋斗云]" ,
                    "[火箭]" ,
                    "[救护车]" ,
                    "[便便]"
    };
    static {

    }
    public final static String filePrefixStart="[file://";
    public final static String filePrefixEnd="]";
    public final static String filePrefix=filePrefixStart+"%s"+filePrefixEnd;
    //匹配已方括号成对出现的数据
    public final static String fileMsgPrefixPattern="(\\"+filePrefixStart+")[^\\[\\[\\]]*?(\\"+filePrefixEnd+")";
    public static int[] getEmojis(){
        return res;
    }
    public static String getEmoji(int position){
        return msg[position];
    }

    public static int getEmojiPosition(String emoji){
        for (int i = 0; i < msg.length; i++) {
            if(emoji.equals(msg[i])){
                return i;
            }
        }
        return -1;
    }
    //将表情下标展示位表情图片
    public static SpannableString getMask(Context context,int postion){
        int drawable=res[postion];
        SpannableString spannableString=new SpannableString(EmojiUtils.getEmoji(postion));
        Bitmap bm=BitmapFactory.decodeResource(context.getResources(),drawable);
        ImageSpan imageSpan=new ImageSpan(context, zoomImg(bm,50,50));
        spannableString.setSpan(imageSpan, 0, EmojiUtils.getEmoji(postion).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
    //将表情下标展示位表情图片
    public static SpannableString getFileMask(Context context,String path){
        String pathFile=String.format(filePrefix,path);
        SpannableString spannableString=new SpannableString(pathFile);
//        Bitmap bm=BitmapFactory.decodeResource(context.getResources(),drawable);
        Bitmap bm=BitmapFactory.decodeFile(path);
        ImageSpan imageSpan=new ImageSpan(context, zoomImg(bm,bm.getWidth()/4,bm.getHeight()/4));
        spannableString.setSpan(imageSpan, 0, pathFile.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
    //将表情下标展示位表情图片
    public static SpannableString getFileMask(Context context,String path,int fileTypeDrawable){
        String pathFile=String.format(filePrefix,path);
        SpannableString spannableString=new SpannableString(pathFile);
        Bitmap bm=BitmapFactory.decodeResource(context.getResources(),fileTypeDrawable);
//        Bitmap bm=BitmapFactory.decodeFile(path);
        ImageSpan imageSpan=new ImageSpan(context, zoomImg(bm,bm.getWidth()/4,bm.getHeight()/4));
        spannableString.setSpan(imageSpan, 0, pathFile.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    //将表情下标展示位表情图片
    public static Bitmap getFile(String path){
        Bitmap bm=BitmapFactory.decodeFile(path);
        return zoomImg(bm,bm.getWidth()/2, bm.getHeight()/2);
    }//将表情下标展示位表情图片
    public static Bitmap getFile(Context context,int path){
        Bitmap bm=BitmapFactory.decodeResource(context.getResources(),path);
        return zoomImg(bm,bm.getWidth()/2,bm.getHeight()/2);
    }

    private static Pattern buildPattern() {
        StringBuilder patternString = new StringBuilder(msg.length * 3);

        patternString.append('(');
        for (String s : msg) {
            patternString.append(Pattern.quote(s));
            patternString.append('|');
        }
        patternString.replace(patternString.length() - 1, patternString.length(), ")");

        return Pattern.compile(patternString.toString());
    }

    private static Pattern buildFilePattern() {
        StringBuilder patternString = new StringBuilder(6);

//        patternString.append('[');
        patternString.append(fileMsgPrefixPattern);
//        patternString.append("]");
        Log.e(TAG, "buildFilePattern: "+ patternString.toString());
        return Pattern.compile(patternString.toString());
    }

    public static List<String> getFilesPathInMsg(String msg){
        Matcher matcher = buildFilePattern().matcher(msg);
        List<String>files=new ArrayList<>();
        while (matcher.find()) {
            String matherIt=matcher.group();
            Log.e(TAG, "getFilesPathInMsg: "+matherIt );
            files.add(matherIt.substring(matherIt.indexOf(filePrefixStart)+filePrefixStart.length(),matherIt.length()-1));
        }
        return files;
    }
    public static String getTextInFileMsg(String msg){
        Matcher matcher = buildFilePattern().matcher(msg);
        while (matcher.find()) {
            String matherIt=matcher.group();
            msg=msg.replace(matherIt,"");
        }
        return msg;
    }

    //将文本消息匹配到图片
    public static SpannableStringBuilder getMask(Context context,String msg){
        SpannableStringBuilder builder = new SpannableStringBuilder(msg);

        Matcher matcher = buildPattern().matcher(msg);
        while (matcher.find()) {
            int mat=getEmojiPosition(matcher.group());
            Bitmap bm=BitmapFactory.decodeResource(context.getResources(),res[mat]);
            ImageSpan imageSpan=new ImageSpan(context, zoomImg(bm,50,50));
            builder.setSpan(imageSpan,matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }

    public static int findIndexFromEnd(String str,int start,char prefxix){
        char[] strs=str.toCharArray();
        for (int i = strs.length - 1; i >= 0; i--) {
            if(strs[i]==prefxix&&i<start){
                return i;
            }
        }
        return start;
    }

    private static Bitmap zoomImg(Bitmap bm, int newWidth , int newHeight){
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = 1.0f*newWidth / width;
        float scaleHeight = 1.0f*newHeight / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
//        Bitmap newbm = Bitmap.createScaledBitmap(bm,newWidth,newHeight,true);
        return newbm;
    }
}
